package StudyCode.summary.zipfile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Adler32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CreateZipFileWithAdler32Checksum {
	public static void main(String args[]) {
		String zipFile = "E:/Copy/zipdemo.zip";
		String sourceFile = "E:/Copy/csv.csv";

		byte[] buffer = new byte[1024];

		try {
			FileOutputStream fout = new FileOutputStream(zipFile);

			CheckedOutputStream checksum = new CheckedOutputStream(fout, new Adler32());

			ZipOutputStream zout = new ZipOutputStream(checksum);

			FileInputStream fin = new FileInputStream(sourceFile);

			zout.putNextEntry(new ZipEntry(sourceFile));

			int length;

			while ((length = fin.read(buffer)) > 0) {
				zout.write(buffer, 0, length);
			}

			zout.closeEntry();

			fin.close();

			zout.close();

			System.out.println("Zip 文件已经被创建!");
			System.out.println("Adler32 检验值是 : " + checksum.getChecksum().getValue());
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}

	}
}
